// 导入koa路由模块
const Router = require('koa-router')
const router = new Router()
// 引入获取token的方法模块
// 引入网络请求模块
const callCloudFn = require('../utils/callCloudFn.js')
const callCloudDB = require('../utils/callCloudDB.js')
const CallCloudStorage = require('../utils/callCloudStorage.js')

// 定义List方法路由  获取贴子列表
// apigw.tencentcs.com/newslist/list
router.get('/List', async (ctx, next) => {
    let resData = []
    // 拼接查询语句
    //  http://localhost:9000/newslist/list?infoTag=报警装置&start=0&count=10  
    let start = parseInt(ctx.request.query.start)
    let count = parseInt(ctx.request.query.count)
    let infoTag = ctx.request.query.infoTag
    let query = `db.collection('news').where({
            infoTag:'${infoTag}'
        })
        .skip(${start})
        .limit(${count})
        .orderBy('createTime', 'desc')
        .get()`
    // let query = `db.collection('news').doc('${ctx.request.query.id}').get()`
    let res = await callCloudDB(ctx, 'databasequery', query)
    // console.log(res, '=================');
    //     errcode: 0,
    //   errmsg: 'ok',
    // errcode: -605101,
    if (res.errcode < 0) {
        return
    }
    let data1 = res.data
    let data = []
    for (i = 0, len = data1.length; i < len; i++) {
        data.push(JSON.parse(data1[i]))
    }
    let dataList = []
    for (let i = 0, len = data.length; i < len; i++) {
        let fileList = []
        let img = []
        if (data[i].img.length > 0) {
            for (let j = 0, len = data[i].img.length; j < len; j++) {
                fileList.push({
                    fileid: data[i].img[j],
                    max_age: 7200
                })
            }
            let dlRes = await CallCloudStorage.downLoad(ctx, fileList)

            for (let k = 0, len = dlRes.file_list.length; k < len; k++) {
                img.push(
                    dlRes.file_list[k].download_url
                )
            }
            data[i].img = img
            resData.push(data[i])
            // console.log('有图片push完之后的数组',resData);
        } else {
            // console.log('这条信息没有图片',data[i]);
            resData.push(data[i])
            // console.log('没有图片push完之后的数组',resData);
        }
    }

    // console.log('总的数组',resData);
    ctx.body = resData
    // ctx.body = data
})

// 定义getById方法路由  获取贴子详情
router.get('/getById', async (ctx, next) => {
    // 拼接查询语句
    let query = `db.collection('news').doc('${ctx.request.query.id}').get()`
    let res = await callCloudDB(ctx, 'databasequery', query)
    // console.log(JSON.parse(res.data));
    // ctx.body = JSON.parse(res.data)
    //http://localhost:9000/newslist/getById?id=2d44d6c2613b7a1c0b8b50856760b7b3
    // 文件下载链接 
    // console.log(res.data.length, '-++++++++++++++++++++++');
    if (res.data.length > 0) {
        // console.log("有数据 判断数据里面有图片内容吗？-----------");
    } else {
        // console.log("没有数据- 返回-----------");
        return;

    }
    let fileList = []
    let data = null
    if (res.data) {
        data = JSON.parse(res.data)
    }
    // console.log(data.img.length, '图片长度------------------');
    if (data.img.length > 0) {
        // console.log('有图片 替换图片链接--------');
        for (let i = 0, len = data.img.length; i < len; i++) {
            fileList.push({
                fileid: data.img[i],
                max_age: 7200
            })
        }
        let dlRes = await CallCloudStorage.downLoad(ctx, fileList)
        let img = []
        for (let i = 0, len = dlRes.file_list.length; i < len; i++) {
            img.push(
                dlRes.file_list[i].download_url
            )
        }
        data.img = img
    }
    ctx.body = data
})
// 定义获取视频分类方法路由  获取视频分类
router.get('/type', async (ctx, next) => {
    // 拼接查询语句
    let query = `db.collection('type').get()`
    let res = await callCloudDB(ctx, 'databasequery', query)

    ctx.body = JSON.parse(res.data)
})
// 定义根据分类名称获取视频数据
router.get('/status', async (ctx, next) => {
    // 拼接查询语句
    let query = null
    let start = parseInt(ctx.request.query.start)
    let count = parseInt(ctx.request.query.count)
    let keyWord = ctx.request.query.keyWord
    if (keyWord !== '全部') {
        query = `db.collection('videos').where({status:'${keyWord}'}).skip(${start}).limit(${count}).orderBy('_createTime','desc').get()`
    } else {
        query = `db.collection('videos').skip(${start}).limit(${count}).orderBy('_createTime','desc').get()`
    }
    // let res = await callCloudDB(ctx, 'databasequery', query)
    // let arr = []
    // for (i = 0, len = res.data.length; i < len; i++) {
    //     arr.push(JSON.parse(res.data[i]))
    // }

    let resdata=[]
    let res = await callCloudDB(ctx, 'databasequery', query)
    let arr = []
    resdata = res.data
    for (i = 0, len = resdata.length; i < len; i++) {
        arr.push(JSON.parse(resdata[i]))
    }
    ctx.body = arr
})

module.exports = router